package org.smallreunion.service;

import org.slim3.datastore.Datastore;
import org.slim3.datastore.S3QueryResultList;
import org.slim3.datastore.SortCriterion;
import org.smallreunion.form.BBSPostForm;
import org.smallreunion.meta.BBSPostMeta;
import org.smallreunion.model.BBSPost;
import org.smallreunion.model.Member;
import org.smallreunion.model.constract.CounterShardType;
import org.smallreunion.util.StringUtil;

import com.google.appengine.api.datastore.Transaction;

/**
 * BBSService
 * 
 * @author Sinmetal
 * 
 */
public class BBSService {

    public final CounterShardType BBS = CounterShardType.BBS;

    public final Integer DEFAULT_LIMIT = 20;

    private BBSPostMeta meta = new BBSPostMeta();

    public final SortCriterion DEFAULT_SORT = meta.serial.desc;

    private CounterShardService counterService = new CounterShardService();

    private MemberService memberService = new MemberService();

    /**
     * 投稿
     * 
     * @param input
     * @return
     */
    public BBSPost entry(BBSPostForm form, long twitterId) {
        Member member = memberService.findById(twitterId);
        counterService.increment(BBS);
        int count = counterService.getCount(BBS);
        BBSPost bbspost = BBSPost.getInstance(form, member, count);
        bbspost.setKey(BBSPost.createKey(count));
        Transaction tx = Datastore.beginTransaction();
        Datastore.put(bbspost);
        tx.commit();
        return bbspost;
    }

    /**
     * 一覧取得
     * 
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostList() {
        return getBBSPostList(DEFAULT_LIMIT);
    }

    /**
     * 一覧取得
     * 
     * @param cursor
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostList(String cursor) {
        if (StringUtil.isEmpty(cursor)) {
            return getBBSPostList(DEFAULT_LIMIT);
        }
        return getBBSPostList(cursor, DEFAULT_LIMIT);
    }

    /**
     * 一覧取得
     * 
     * @param limit
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostList(int limit) {
        return Datastore
            .query(meta)
            .limit(limit)
            .sort(DEFAULT_SORT)
            .asQueryResultList();
    }

    /**
     * 一覧取得
     * 
     * @param cursor
     * @param limit
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostList(String cursor, int limit) {
        return Datastore
            .query(meta)
            .encodedStartCursor(cursor)
            .limit(limit)
            .sort(DEFAULT_SORT)
            .asQueryResultList();
    }

    /**
     * 指定されたHashtagに該当するPost全件取得
     * 
     * @param hashtag
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostListByHashtag(String hashtag) {
        return Datastore
            .query(meta)
            .filter(meta.hashtags.equal(hashtag))
            .sort(DEFAULT_SORT)
            .asQueryResultList();
    }

    /**
     * 指定されたHashtagに該当するPost全件取得
     * 
     * @param hashtag
     * @param cursor
     * @return
     */
    public S3QueryResultList<BBSPost> getBBSPostListByHashtag(String hashtag,
            String cursor) {
        return Datastore
            .query(meta)
            .filter(meta.hashtags.equal(hashtag))
            .encodedStartCursor(cursor)
            .sort(DEFAULT_SORT)
            .asQueryResultList();
    }

    /**
     * カウント
     * 
     * @return
     */
    public int count() {
        return Datastore.query(meta).count();
    }
}
